using System.Drawing;
using System.Text;
using System.Web;
using Kaleida.ServiceMonitor.Model.Email;

namespace Kaleida.ServiceMonitor.Model.Runtime
{
    public static class MonitorStateExtensions
    {
        public static SeverityLevel ToSeverityLevel(this MonitorState state)
        {
            if (!state.IsMonitoring)
                return SeverityLevel.NotMonitoring;

            if (state.OperationErrorCount == 0)
                return SeverityLevel.Operational;
            if (state.OperationErrorCount <= 1)
                return SeverityLevel.Temperamental;
            if (state.OperationErrorCount <= 10)
                return SeverityLevel.Unstable;
            if (state.OperationErrorCount <= 100)
                return SeverityLevel.Critical;

            return SeverityLevel.Dire;

        }
        
        public static Color ToColour(this SeverityLevel level)
        {
            switch (level)
            {
                case SeverityLevel.Operational:
                    return Color.LightGreen;
                case SeverityLevel.Temperamental:
                    return Color.Yellow;
                case SeverityLevel.Unstable:
                    return Color.Orange;
                case SeverityLevel.Critical:
                    return Color.LightCoral;
                case SeverityLevel.Dire:
                    return Color.Crimson;
                default:
                    return Color.Gray;
            }
        }

        public static string ToSummary(this SeverityLevel severityLevel)
        {
            return string.Format("{0} {1} ({2})", (int)severityLevel, severityLevel, severityLevel.ToColour().Name);
        }
        
        public static IHtmlString ToSummaryHtml(this SeverityLevel severityLevel)
        {
            var colour = severityLevel.ToColour();
            var htmlColour = ColorTranslator.ToHtml(colour);

            var html = new StringBuilder();
            html.AppendFormat("<span style='padding:4px; background-color: {0};'>", htmlColour);
            html.AppendFormat("{0} {1}", (int)severityLevel, severityLevel.ToString().HtmlEncode());
            html.AppendFormat(" ({0})</span>", colour.Name);
            return html.ToString().AsRawHtml();
        }
    }
}